package com.sinevil.tcpTrans.entity.task;

import com.sinevil.tcpTrans.entity.json.BasicJson;
import com.sinevil.tcpTrans.entity.json.InstructionJson;
import lombok.SneakyThrows;

import java.nio.charset.StandardCharsets;

import static com.sinevil.tcpTrans.entity.json.handlers.SocketJsonHandle.objectMapper;

/**
 * @Classname InstructionTask
 * @Description TODO
 * @Version 1.0.0
 * @Date 2025/1/5 15:35
 * @Created by sinevil
 */
public class InstructionTask extends Task {

    @SneakyThrows
    public InstructionTask(BasicJson instruction) {
        // 指令任务优先级设为1，数值越小优先级越高，这里表示指令优先级最高
        super(16);
        String jsonString = objectMapper.writeValueAsString(instruction);
        this.bytes = transToBytes(jsonString);
    }
    @SneakyThrows
    public InstructionTask(InstructionJson instruction) {
        // 指令任务优先级设为1，数值越小优先级越高，这里表示指令优先级最高
        super(16);
        String jsonString = objectMapper.writeValueAsString(instruction);
        this.bytes = transToBytes(jsonString);
    }

    @Override
    public String toString() {
        bytes.flip();
        String jsonString  = StandardCharsets.UTF_8.decode(bytes).toString();
        return String.format("Task{priority:%d, instruction:%s}", priority, jsonString);
    }
}